package com.shijianchen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shijianchen.mapper.SpMenuMapper;
import com.shijianchen.projo.SpMenu;
import com.shijianchen.projo.vos.MetaVo;
import com.shijianchen.projo.vos.UserRoute;
import com.shijianchen.service.SpMenuService;
import com.shijianchen.utils.MenuTreeElement;
import com.shijianchen.utils.RouteTreeElement;
import com.shijianchen.utils.TreeElement;
import com.shijianchen.utils.TreeUtils;
import org.json.JSONArray;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SpMenuServiceImpl extends ServiceImpl<SpMenuMapper, SpMenu> implements SpMenuService {

    @Override
    public Integer updateMenu(SpMenu spMenu) {
        return baseMapper.insert(spMenu);
    }

    @Override
    public Boolean updateBatchMenu(ArrayList<SpMenu> spMenuArrayList) {
        return this.saveBatch(spMenuArrayList);
    }

    @Override
    public List<TreeElement<UserRoute>> getUserRoutes() throws IllegalAccessException {
        LambdaQueryWrapper<SpMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<SpMenu> spMenus = baseMapper.selectList(lambdaQueryWrapper);
        System.out.println(new JSONArray(spMenus));
        TreeUtils<SpMenu,UserRoute> spTreesUserRouteTreeUtils = new TreeUtils<>((SpMenu spMenu) -> {
            RouteTreeElement<UserRoute> userRouteTreeElement = new RouteTreeElement<>();
            userRouteTreeElement.setName(spMenu.getRouteName());
            userRouteTreeElement.setId(spMenu.getId());
            userRouteTreeElement.setPath(spMenu.getRoutePath());
            userRouteTreeElement.setComponent(spMenu.getComponent());
            MetaVo metaVo = new MetaVo();
            metaVo.setIcon(spMenu.getIcon());
            metaVo.setOrder(spMenu.getSort());
            metaVo.setI18nKey(spMenu.getI18nKey());
            metaVo.setTitle(spMenu.getMenuName());
            metaVo.setRoles(new String[]{"R_ADMIN"});
            userRouteTreeElement.setMeta(metaVo);
            if(spMenu.getHideInMenu() == 0){
                metaVo.setHideInMenu(true);
            }else{
                metaVo.setHideInMenu(false);
            }
            return userRouteTreeElement;
        });
        return spTreesUserRouteTreeUtils.buildTree(spMenus);
    }

    @Override
    public List<TreeElement<SpMenu>> getMenuList() throws IllegalAccessException {
        LambdaQueryWrapper<SpMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(SpMenu::getSort);
        List<SpMenu> spMenus = baseMapper.selectList(wrapper);
        TreeUtils<SpMenu,SpMenu> spTreesUserRouteTreeUtils = new TreeUtils<>((SpMenu spMenu) -> {
            MenuTreeElement<SpMenu> menuTreeElement = new MenuTreeElement<>();
            menuTreeElement.setMenuName(spMenu.getMenuName());
            menuTreeElement.setMenuType(spMenu.getMenuType());
            menuTreeElement.setHideInMenu(spMenu.getHideInMenu());
            menuTreeElement.setIcon(spMenu.getIcon());
            menuTreeElement.setSort(spMenu.getSort());
            menuTreeElement.setStatus(spMenu.getStatus());
            menuTreeElement.setUpdateBy(spMenu.getUpdateBy());
            menuTreeElement.setComponent(spMenu.getComponent());
            menuTreeElement.setCreateBy(spMenu.getCreateBy());
            menuTreeElement.setRouteName(spMenu.getRouteName());
            menuTreeElement.setRoutePath(spMenu.getRoutePath());
            menuTreeElement.setI18nKey(spMenu.getI18nKey());
            menuTreeElement.setIconType(spMenu.getIconType());
            return menuTreeElement;
        });
        return spTreesUserRouteTreeUtils.buildTree(spMenus);
    }

    @Override
    public Integer addOrUpdateMenu(SpMenu spMenu,String type) {
        return switch (type) {
            case "edit" -> baseMapper.updateById(spMenu);
            case "add" -> baseMapper.insert(spMenu);
            default -> 0;
        };
    }
}
